<template>
  <div>
    <!-- 忘记密码 -->
    <el-dialog @close="close" v-model="dialogvisible" title="忘记密码" width="30%">
      <el-form
        ref="forgetPasswordFormRef"
        class="login-form"
        :model="forgetPasswordForm"
        :rules="ForgetRules"
      >
        <el-form-item prop="account" label="账号">
          <el-input
            v-model="forgetPasswordForm.account"
            placeholder="请输入您的注册账号"
          ></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="forgetPasswordForm.email" placeholder="请输入您的邮箱"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" size="default" @click="dialogvisible = false">取消</el-button>
        <el-button type="primary" size="default" @click="openNew">下一步</el-button>
      </template>
    </el-dialog>
    <!-- 修改密码 -->
    <el-dialog @close="close" v-model="ChangePassword" title="忘记密码" width="30%">
      <el-form
        ref="changePasswordFormRef"
        class="login-form"
        :model="changePasswordForm"
        :rules="changePasswordRules"
      >
        <el-form-item prop="newPassword" label="密码">
          <el-input
            type="password"
            v-model="changePasswordForm.newPassword"
            placeholder="请输入您的新密码"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认" prop="repPassword">
          <el-input
            type="password"
            v-model="changePasswordForm.repPassword"
            placeholder="确认您的新密码"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" size="default" @click="ChangePassword = false">取消</el-button>
        <el-button type="primary" size="default" @click="submit">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { resetPasswordAPI, verifyAPI } from '@/api/login'
import type { FormRules } from 'element-plus'
import { reactive, ref } from 'vue'

const forgetPasswordFormRef = ref()
const changePasswordFormRef = ref()

const dialogvisible = ref<boolean>(false) //弹窗显示
const ChangePassword = ref<boolean>(false) //修改密码弹窗显示
// 忘记密码表单数据
const forgetPasswordForm = reactive<LoginForm>({
  account: null,
  email: ''
})
// 修改密码表单数据
const changePasswordForm = reactive<LoginForm>({
  newPassword: '',
  repPassword: ''
})
// 打开忘记密码弹窗
const open = () => {
  dialogvisible.value = true
}
// 打开修改密码弹窗
const openNew = async () => {
  await forgetPasswordFormRef.value.validate()
  const res = await verifyAPI(forgetPasswordForm)
  changePasswordForm.id = res.data
  dialogvisible.value = false
  ChangePassword.value = true
}

// 自定义校验规则
const validatePass2 = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请确认您的新密码'))
  } else if (value !== changePasswordForm.newPassword) {
    callback(new Error('两次密码输入不一致'))
  } else {
    callback()
  }
}

// 忘记密码校验规则
const ForgetRules = reactive<FormRules<LoginForm>>({
  account: [
    { required: true, message: '请输入您的账号', trigger: 'blur' },
    { min: 6, max: 12, message: '账号必须是6-12位的数字', trigger: 'blur' }
  ],
  email: [{ required: true, message: '请输入您的邮箱', trigger: 'blur' }]
})
// 修改密码校验规则
const changePasswordRules = reactive<FormRules<LoginForm>>({
  newPassword: [{ required: true, message: '请输入您的新密码', trigger: 'blur' }],
  repPassword: [
    { validator: validatePass2, trigger: 'blur' },
    { required: true, message: '请输入您的新密码', trigger: 'blur' }
  ]
})

// 表单关闭前的回调
const close = () => {
  // 清除表单内容及校验
  forgetPasswordFormRef.value?.resetFields()
  changePasswordFormRef.value?.resetFields()
}

// 提交
const submit = async () => {
  await changePasswordFormRef.value.validate()
  await resetPasswordAPI(changePasswordForm)
  ChangePassword.value = false
}

defineExpose({
  open
})
</script>

<style scoped></style>
